//
//  FGLastestVoucherViewController.m
//  Mi6App
//
//  Created by Mr.Lemon on 8/30/13.
//  Copyright (c) 2013 4G Secure. All rights reserved.
//

#import "FGLastestVoucherViewController.h"

@interface FGLastestVoucherViewController ()

@end

@implementation FGLastestVoucherViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self loadDemoData];
    [self loadLastestVoucher];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark load Data
-(void)loadDemoData{
    
    FGVoucherInfo *voucher1=[[FGVoucherInfo alloc] initWithId:@"1" title:@"Sains Burys" desc:@"Sains Burys Name" url_image:@"NewVoucher_Sainsburys.png" andExpiredDate:@""];
    FGVoucherInfo *voucher2=[[FGVoucherInfo alloc] initWithId:@"1" title:@"Majestic" desc:@"Majestic Name" url_image:@"NewVoucher_Majestic.png" andExpiredDate:@""];
    FGVoucherInfo *voucher3=[[FGVoucherInfo alloc] initWithId:@"1" title:@"Pizza" desc:@"Pizza Name" url_image:@"NewVoucher_Pizza.png" andExpiredDate:@""];
    FGVoucherInfo *voucher4=[[FGVoucherInfo alloc] initWithId:@"1" title:@"TrackDay " desc:@"TrackDay Name" url_image:@"NewVoucher_TrackDay.png" andExpiredDate:@""];
    FGVoucherInfo *voucher5=[[FGVoucherInfo alloc] initWithId:@"1" title:@"Yoda" desc:@" Yoda Name" url_image:@"NewVoucher_Yoda.png" andExpiredDate:@""];
    self.arrLastVoucher=[[NSMutableArray alloc] initWithObjects:voucher1,voucher2,voucher3,voucher4,voucher5, nil];
    
    
}
#pragma mark - Load UI
- (void)loadLastestVoucher {
    for (int i = 0; i < self.arrLastVoucher.count; i++) {
        
        NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"FGLastestVoucherView" owner:self options:nil];
        FGLastestVoucherView *voucherView = [nibViews objectAtIndex:0];
        FGVoucherInfo *voucherInfo = [self.arrLastVoucher objectAtIndex:i];
        voucherView.frame = CGRectMake(scrollViewMain.frame.size.width*i, 0, scrollViewMain.frame.size.width, scrollViewMain.frame.size.height);
        
        NSString *urlImg = [NSString stringWithFormat:@"%@%@",kAPIBaseURKImage,voucherInfo.urlImage];
        //        [voucherView.imgVoucher setImageWithURL:[NSURL URLWithString:urlImg] placeholderImage:nil];
        [voucherView.imgVoucher setImage:[UIImage imageNamed:voucherInfo.urlImage]];
        voucherView.lblVoucherTitle.text = voucherInfo.title;
        voucherView.lblVoucherName.text=voucherInfo.description;
        [scrollViewMain addSubview:voucherView];
    }
    
    pageControl.numberOfPages = self.arrLastVoucher.count;
    
    [scrollViewMain setContentSize:CGSizeMake(scrollViewMain.frame.size.width*self.arrLastVoucher.count, scrollViewMain.frame.size.height)];
}

- (void)localizedLanguage{
    self.title = LocalizedString(@"Khuyến mãi");
}

#pragma mark - Scrollview delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView {
    int index = scrollView.contentOffset.x / scrollView.frame.size.width;
    pageControl.currentPage = index;
}



- (void)viewDidUnload {
    scrollViewMain = nil;
    pageControl = nil;
    scrollViewMain = nil;
    pageControl = nil;
    [super viewDidUnload];
}


@end
